package uebung9;

import uebung9.*;

public class PoorMansExcel {
	private int fieldLength = 0, pos = 0;
	private Entry[] table;
	private Entry anentry;
	private StringBuilder out= new StringBuilder();
	

	public PoorMansExcel(int p) {
		fieldLength = p;
		table = new Entry[fieldLength];
	}

	public PoorMansExcel(Entry[] en) {
		table = en;
		fieldLength = table.length;

	}

	public int getSize() {
		return fieldLength;
	}

	public void resize(int newLength) {

		if (newLength > 0) {
			fieldLength = newLength;
			Entry[] fields = table; // Backup von table
			table = new Entry[fieldLength]; // Loeschen von table und neues
			// leeres Array
			for (int i = 0; i < fields.length && i < table.length; i++) {
				table[i] = fields[i];
			}
		}
	}

	public boolean update(int p, Entry ae) {
		pos = p;
		anentry = ae;

		if (pos > fieldLength)
			return false;
		else
			table[pos - 1] = anentry;
		return true;
	}

	public String toString() {
		for (int i = 0; i < fieldLength; i++) {
			if (i < 9)
				out = out.append("  ");  //leerzeichen vorne//
			else {
				if (i < 99)
					out = out.append(" ");
				
			}

			out = out.append(i + 1).append(" ");
			if (table[i].getDescription().length() > 60)
				out = out.append(table[i].getDescription().substring(0, 60)); //abschneiden, falls zulang//
			else {
				out = out.append(table[i].getDescription());
				int free = 60 - table[i].getDescription().length();//leerzeichen z�hlen und anh�ngen//
				for (int c = 1; c <= free; c++)
					out = out.append(" ");
			}
			out = out.append(table[i].getValue().evaluate()).append('\n'); 

		}
		return (out.toString());
	}
}
